#include "stm32f10x.h"
#include "delay.h"
#include "debug.h"
uint16_t count = 0;
void pwm_timer_init(void);
void pwm_set(uint8_t dutyCycle);
int main(void)
{
    uart_init();
    pwm_timer_init();
    uint8_t dutyCycle = 1;
    int8_t step = 1;
    while (1)
    {
        if (dutyCycle <= 0 || dutyCycle >= 99)
        {
            step = -step;
        }
        dutyCycle += step;
        pwm_set(dutyCycle);
        delay_ms(10);
    }
}
void pwm_timer_init(void)
{
    RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
    GPIOA->CRL |= (GPIO_CRL_CNF1_1 | GPIO_CRL_MODE1);
    GPIOA->CRL &= ~GPIO_CRL_CNF1_0;
    TIM5->PSC = 7200 - 1;
    TIM5->ARR = 100 - 1;
    TIM5->CR1 &= ~TIM_CR1_DIR;
    TIM5->CCR2 = 20;
    TIM5->CCMR1 &= ~TIM_CCMR1_CC2S;
    TIM5->CCMR1 |= TIM_CCMR1_OC2M_2;
    TIM5->CCMR1 |= TIM_CCMR1_OC2M_1;
    TIM5->CCMR1 &= ~TIM_CCMR1_OC2M_0;
    TIM5->CCER |= TIM_CCER_CC2E;
    TIM5->CCER &= ~TIM_CCER_CC2P;
    TIM5->CR1 |= TIM_CR1_CEN;
}
void pwm_set(uint8_t dutyCycle)
{
    TIM5->CCR2 = dutyCycle;
}
/****************************************   hal  ****************************************/
// void main(void)
// {
//     HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_2);
//     uint8_t dutyCycle = 1;
//     int8_t step = 1;
//     while (1)
//     {
//         if (dutyCycle <= 0 || dutyCycle >= 99)
//         {
//             step = -step;
//         }
//         dutyCycle += step;
//         set_pwm(dutyCycle);
//         HAL_Delay(10);
//     }
// }
// void set_pwm(uint16_t dutyCycle)
// {
//     __HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_2, dutyCycle);
// }
